package service

import (
    "context"
    "errors"
    "log"

    "giikin.oder/db"
    "giikin.oder/kitex_gen/com/giikin/order"
    "gorm.io/gorm"
)

func SaveOrderEtc(ctx context.Context, request *order.SaveOrderEtcRequest) error {
    _, err := db.GetOrderEtcInfo(ctx, request.GetOrderID(), request.GetType())
    if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
        log.Println(err)
        return err
    }

    if errors.Is(err, gorm.ErrRecordNotFound) {
        // 添加
        err = db.AddOrderEtc(ctx, request.GetOrderID(), request.GetType(), request.GetValue(), request.GetSubData())
        if err != nil {
            log.Println(err)
            return err
        }

        return nil
    }

    // 修改
    err = db.UpdateOrderEtc(ctx, request.GetOrderID(), request.GetType(), request.GetValue(), request.GetSubData())
    if err != nil {
        log.Println(err)
        return err
    }

    return nil
}
